package ac.biu.nlp.nlp.curator.client.rtesum;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.thrift.TException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import ac.biu.nlp.nlp.curator.client.ClientTreesXml;
import ac.biu.nlp.nlp.curator.client.CuratorClientException;

import edu.illinois.cs.cogcomp.thrift.base.AnnotationFailedException;
import edu.illinois.cs.cogcomp.thrift.base.ServiceUnavailableException;
import eu.excitementproject.eop.common.utilities.datasets.rtesum.CorpusDocumentReader;
import eu.excitementproject.eop.common.utilities.datasets.rtesum.DefaultCorpusDocumentReader;
import eu.excitementproject.eop.common.utilities.datasets.rtesum.Rte6mainIOException;

/**
 * 
 * @author Asher Stern
 * @since Mar 24, 2013
 *
 */
public class RteSumDocumentSrl
{
	public static final ArrayList<String> ANNOTATORS_NAME = new ArrayList<String>();
	static
	{
		ANNOTATORS_NAME.add("srl");
		ANNOTATORS_NAME.add("nom");
		ANNOTATORS_NAME.trimToSize();
	}
			
	
	public RteSumDocumentSrl(File rteDocument, Document xmlDocument,
			Element xmlElement, String host, int port)
	{
		super();
		this.rteDocument = rteDocument;
		this.xmlDocument = xmlDocument;
		this.xmlElement = xmlElement;
		this.host = host;
		this.port = port;
	}

	public void process() throws ServiceUnavailableException, AnnotationFailedException, TException, Rte6mainIOException, CuratorClientException
	{
		appendAllSentences(getSentences());
	}
	
	private Map<Integer,String> getSentences() throws Rte6mainIOException
	{
		CorpusDocumentReader reader = new DefaultCorpusDocumentReader();
		reader.setXml(rteDocument.getPath());
		reader.read();
		Map<Integer,String> sentences = reader.getMapSentences();
		return sentences;
	}
	
	private void appendAllSentences(Map<Integer,String> sentences) throws ServiceUnavailableException, AnnotationFailedException, TException, CuratorClientException
	{
		List<Integer> sentencesIndexes = new ArrayList<Integer>(sentences.keySet().size());
		sentencesIndexes.addAll(sentences.keySet());
		Collections.sort(sentencesIndexes);
		for (Integer sentenceId : sentencesIndexes)
		{
			String sentence = sentences.get(sentenceId);
			try
			{
				//System.out.println("\t\tProcess sentence "+sentenceId);
				Element sentenceElement = xmlDocument.createElement("sentence");
				xmlElement.appendChild(sentenceElement);
				sentenceElement.setAttribute("sentenceId", String.valueOf(sentenceId));
				Element textElement = xmlDocument.createElement("text");
				textElement.appendChild(xmlDocument.createTextNode(sentence));
				sentenceElement.appendChild(textElement);

				Element srlElement = xmlDocument.createElement("srl");
				sentenceElement.appendChild(srlElement);
				if (SkipList.skip.contains(sentence))
				{
					System.out.println("\t\tSkip sentence: <<"+sentence+">>");
				}
				else
				{
					ClientTreesXml client = new ClientTreesXml(host,port);
					client.process(xmlDocument, srlElement, sentence, ANNOTATORS_NAME);
				}
			}
			catch(Exception e)
			{
				System.out.println("Failed in processing sentence "+sentenceId+":\n"+
						sentence);
				throw e; // works for JDK >= 1.7
			}

		}
	}

	private File rteDocument;
	private Document xmlDocument;
	private Element xmlElement;
	private String host;
	private int port;
	
	
}
